﻿@model MvcCms.Web.ViewModels.HomeModel

@section HtmlHead{
    <link href="@Url.Content("/content/start1.8/jquery-ui-1.8.11.custom.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("/RazorThemes/" + Model._CurrentPortal.SkinName + "/modal/jqModal.css")" rel="stylesheet" type="text/css" />
    <meta name="description" content="@Model._ContentPage.Description" />
    <meta http-equiv="X-XRDS-Location" content="http://@ViewData["SiteURL"]/xrds.aspx"/>
}
<table>
    <tr>
@if (Roles.IsUserInRole("Editor"))
{    

        <td align="left">
            <a href="/backoffice/editcontentpage/@Model._ContentPage.ContentPageID">
            <img alt="cmseditor" border="0" src="/content/images/UEB16.png" /></a>        
        </td> 
}
@if (Model._ContentPage.ShowContributorLink)
{
        <td align="right">
                <text>Contributor: @Html.Raw(Model._ContentPage.Contributor.UserName.ToProfileLink())</text>            
        </td>
}                
    </tr>
    </table>
<div class="contentpage-outerdiv">
    @MvcHtmlString.Create(ViewData["PageContent"].ToString())
    <div>    
    @if (Model._AltLangueVersions.Count() > 0)
    {
        @Html.Partial("ControlsCMS/AltLanguageVersions", Model._AltLangueVersions)   
    }
    @if (Model._ContentPage.CommentsEnabled || Model._ContentPage.AnonCommentsEnabled)
    {
        @Html.Partial("ControlsCMS/CommentsContainer", Model) 
    }
    </div>
</div>
@Html.Hidden("moduleholder", "")
@Html.Hidden("defaultLanguageId", ViewData["DefaultLangID"])
@Html.Hidden("SiteName", ViewData["SiteName"])
@Html.Hidden("currentPageId", Model._ContentPage.ContentPageID)

@section ScriptContent{  
    <script src="@Url.Content("/Scripts/jquery-ui-1.8.11.custom.min.js")" type="text/javascript"></script>  
    <script type="text/javascript" > 
        $(document).ready(function () {
            $("#altlangisloading").hide();
            @if (Request.Browser.Browser == "IE")
            {
                if (Request.Browser.MajorVersion > 6)
                {
                    <text>LoadModules(@Model._ContentPage.ContentPageID);</text>
                }
            }
            else
            {
                <text>LoadModules(@Model._ContentPage.ContentPageID);</text>
            }
            
            @foreach (var weightedContentModule in Model._WeightedContentsModuleType)
            {
                var intervalTime = weightedContentModule.IntervalSeconds * 1000;
                var firstTransitionDelay = weightedContentModule.FirstTransitionDelay * 1000;
                var targetDiv = weightedContentModule.TargetDiv;
                <text>
                //ok we need to add these to the weightedContentModule array and since there is one
                //make the timing function that calls out to show the content
                //we will set the interval after the first fetch timed off the FirstTransitionDelay 
                //setting on the weighted content settings
                $('body').delay(@firstTransitionDelay).queue(function(){
                    RotateWeightedContentFirst('@targetDiv', @intervalTime);
                });                
                </text>                
            }
        });
    </script>
    <script id="RotateWeightedContent()" type="text/javascript">
        function RotateWeightedContentFirst(targetDiv, interval) {
            //if the target div exists on this page then ask the server for the next content bit
            if ($("#" + targetDiv).length > 0) {
                $.post("/mvccmsAjax/GetWeightedModuleContent/0",
			    { targetDiv: targetDiv },
			    function (data) {
			        if (data.error) {
			            //alert(data.error);
			        }
			        else {
			            ReplaceModuleContent(data, targetDiv);
			            window.setInterval(function () { RotateWeightedContent(targetDiv); }, interval);
			        }
			    },
			    "json");
            }
        }
        function RotateWeightedContent(targetDiv) {
            //if the target div exists on this page then ask the server for the next content bit
            if ($("#" + targetDiv).length > 0) {
                $.post("/mvccmsAjax/GetWeightedModuleContent/0",
			    { targetDiv: targetDiv },
			    function (data) {
			        if (data.error) {
			            //alert(data.error);
			        }
			        else {
			            ReplaceModuleContent(data, targetDiv);
			        }
			    },
			    "json");
            }
        }
        function ReplaceModuleContent(newContent, targetDiv) {
            $("#" + targetDiv).hide(newContent.transitionHideEffect, {}, newContent.hideDuration, function () { ShowReplacedModuleContent(newContent, targetDiv); });
        }
        function ShowReplacedModuleContent(newContent, targetDiv) {
            $("#" + targetDiv).html(newContent.html);
            $("#" + targetDiv).show(newContent.transitionShowEffect, {}, newContent.showDuration);
        }
    </script>
    <script src="@Url.Content("/RazorThemes/" + Model._CurrentPortal.SkinName + "/Scripts/jqModal.js")" type="text/javascript"></script>   
    <script type="text/javascript" > 
        $().ready(function() {
            $('#comment-editPH').jqm({          
            ajax: '/mvccmsModal/EditComment/12/4',          
            target: 'div.jqmAlertContent',
            overlay: 30, /* 0-100 (int) : 0 is off/transparent, 100 is opaque */
            overlayClass: 'whiteOverlay'})
            .jqDrag('.jqDrag'); /* make dialog draggable, assign handle to title */
            
            $('#comment-replyPH').jqm({          
            ajax: '/mvccmsModal/CommentReply/12/4',          
            target: 'div.jqmAlertContent',
            overlay: 30, /* 0-100 (int) : 0 is off/transparent, 100 is opaque */
            overlayClass: 'whiteOverlay'})
            .jqDrag('.jqDrag'); /* make dialog draggable, assign handle to title */            
          
            // Close Button Highlighting. IE doesn't support :hover. Surprise?
            $('input.jqmdX')
            .hover(
            function(){ $(this).addClass('jqmdXFocus'); }, 
            function(){ $(this).removeClass('jqmdXFocus'); })
            .focus( 
            function(){ this.hideFocus=true; $(this).addClass('jqmdXFocus'); })
            .blur( 
            function(){ $(this).removeClass('jqmdXFocus'); });
        });         
        
        function OpenCommentModal(commentId)
        {
            var now = new Date();  
            //update parameters          
            $('#comment-editPH').jqm({
                ajax: '/mvccmsModal/EditComment/'+commentId+'/'+now.getMilliseconds()
            });
            //open modal
            $('#comment-editPH').jqmShow();
        }                   
        function InsertReply(commentId)
        {
            var now = new Date();  
            //update parameters          
            $('#comment-replyPH').jqm({
                ajax: '/mvccmsModal/CommentReply/'+commentId+'/'+now.getMilliseconds()
            });
            //open modal
            $('#comment-replyPH').jqmShow();            
        }
     
        GetComments(@Model._ContentPage.ContentPageID);  
        function CreateComment() {
            var data = { 
                AddedByAnonName: $('#AddedByAnonName').val(), 
                AddedByAnonEmail: $('#AddedByAnonEmail').val(),     
                Body: $('#body').val() 
            }
            var encoded = JSON.stringify(data);   
            var now = new Date();         
		    $.post(
			    "/mvccmsAjax/CreateContentComment/" + now.getMilliseconds(),
			    { data: encoded, pageId: @Model._ContentPage.ContentPageID },
			    function (data) {
				    if (data.error){
				        alert(data.error);
				    }
				    else{
				        $("#comments tbody").html("");
				        PopulateComments(data);
				        $('#body').val("");
				    }
			    },
			    "json"
		    );            
            
        }
        function GetComments(pageId) {
        $("#isloading").show();
        var now = new Date();
		    $.post(
			    "/mvccmsAjax/GetContentComments/" + now.getMilliseconds(),
			    { pageId: @Model._ContentPage.ContentPageID },
			    function (data) {
				    if (data.error){
				        //alert(data.error);
				    }
				    else{
				        $("#comments tbody").html("");
				        PopulateComments(data);
				        $("#isloading").hide();
				    }
			    },
			    "json"
		    ); 
        }  
        function CommentReply()
        {
            var now = new Date();  
            var data = { CommentID: $('#parent-commentid').val(), AddedByAnonName: $('#AddedByAnonName').val(), Body: $('#Reply-Body').val() }
            var encoded = JSON.stringify(data);            
		    $.post(
			    "/mvccmsAjax/PostCommentReply/"+now.getMilliseconds(),
			    { data: encoded, pageId: @Model._ContentPage.ContentPageID },
			    function (data) {
				    if (data.error){
				        alert(data.error);
				    }
				    else{
				        $("#comments tbody").html("");
				        PopulateComments(data);
				        $('#comment-replyPH').jqmHide();
				    }
			    },
			    "json"
		    );             
        }
        function EditComment() {
            var data = { CommentID: $('#commentid').val(), AddedByIP: $('#AddedByIP').val(), Body: $('#Body').val(), AddedByAnonName: $('#AddedByAnonName').val(), Approved: $("#Approved:checked").val() }
            var now = new Date();  
            var encoded = JSON.stringify(data); 
            
		    $.post(
			    "/mvccmsAjax/UpdateComment/"+now.getMilliseconds(),
			    { data: encoded },
			    function (data) {
				    if (data.error){
				        alert(data.error);
				    }
				    else{
				        $("#comments tbody").html("");
				        PopulateComments(data);
				        $('#comment-editPH').jqmHide();
				    }
			    },
			    "json"
		    );            
        }        

        function PopulateComments(data){            
		    $.each(data, function(i,comment){
		        var displaydate = comment.ElapsedTime + " ago";
		        var header, headername, gravatar="";
		        if (comment.AddedByAnonEmail!=null){
		            gravatar = "<img src='"+comment.AddedByAnonEmail+"'>";
		        }
		        if (comment.UserName == "anonymous"){
		            if (comment.AddedByAnonName==""){
		                comment.AddedByAnonName="anonymous";
		            }
		            header = "<td><div class='comment-header'>Posted by <span class='name'>"+comment.AddedByAnonName+"</span> "+gravatar+" "+displaydate;
		        }
		        else{
		            header = "<td><div class='comment-header'>Posted by <span class='name'>"+comment.UserName+"</span> "+gravatar+" "+displaydate;
		        }
		        
			    var tblRow =
				    "<tr>"				    
				    +header
				    @if (Roles.IsUserInRole("Editor"))
        {
				        <text>+"&nbsp;&nbsp;<a class='ec"+comment.CommentID+"' href='javascript:void(0);' onclick='OpenCommentModal("+comment.CommentID+")'><span class='xxsmall'>Edit</span></a>"</text>
        }
				    @if (Roles.IsUserInRole("User"))
        {
				        <text>+"&nbsp;&nbsp;<a class='r2c"+comment.CommentID+"' href='javascript:void(0);' onclick='InsertReply("+comment.CommentID+")'><span class='xxsmall'>Reply</span></a>"</text>
        }				    
				    +"</div></td>"
				    +"</div></tr>"
			    $(tblRow).appendTo("#comments tbody");
			    var tblRow2 =
				    "<tr>"
				    +"<td><div><blockquote class='body'>"+comment.Body+"</blockquote></div></td>"
				    +"</tr>";
			    $(tblRow2).appendTo("#comments tbody");	
			    
                $.each(comment.Replies, function(i, reply) {
                    PopulateReply(reply, 1);  
                    $.each(reply.Replies, function(i, reply2) {
                        PopulateReply(reply2, 2);
                        $.each(reply2.Replies, function(i, reply3) {
                            PopulateReply(reply3, 3);                            
                            $.each(reply3.Replies, function(i, reply4) {
                                PopulateReply(reply4, 4);
                            });                            
                        });                        
                    });			    		                                          
                });			    		                    
		    });
        }      
        function PopulateReply(reply, depth){
            var gravatar = "<img src='"+reply.AddedByAnonEmail+"'>";
            var displaydate = reply.ElapsedTime + " ago";          
            var tblRow = "<tr><td align='right'>";
            tblRow +="<div class='commentreply-container' style='width:"+(768-(depth*20))+"px'>";
            tblRow += "<div class='comment-header'>Reply by <span class='name'>"+reply.UserName+"</span> "+gravatar+" "+displaydate;
            
            @if (Roles.IsUserInRole("Editor"))
            {
                <text>tblRow += "&nbsp;&nbsp;<a class='ec" + reply.CommentID + "' href='javascript:void(0);' onclick='OpenCommentModal(" + reply.CommentID + ")'><span class='xxsmall'>Edit</span></a>";</text>
            }	
            if (depth <4){
		        @if (Roles.IsUserInRole("User"))
          {
                    <text>tblRow += "&nbsp;&nbsp;<a class='r2c" + reply.CommentID + "' href='javascript:void(0);' onclick='InsertReply(" + reply.CommentID + ")'><span class='xxsmall'>Reply</span></a>";</text>
          }        
		    }
            tblRow += "</div><div><blockquote class='body'>"+reply.Body+"</blockquote></div>";
            tblRow +="</div>";            
            tblRow += "</td></tr>";
            $(tblRow).appendTo("#comments tbody");        
        }
        function PopulateComment(data){        
            var comment = data;
	        var postdate = jsonParseDate(comment.AddedDate); 
	        var displaydate = getCalendarDate(postdate) + " " + getClockTime(postdate);
		    var tblRow =
			    "<tr>"
			    +"<td><div class='comment-header'>Comment posted by <span class='name'>"+comment.UserName+"</span> "+displaydate+"</div></td>"
			    +"</tr>"
		    $(tblRow).appendTo("#comments tbody");
		    var tblRow2 =
			    "<tr>"
			    +"<td><blockquote class='body'>"+comment.Body+"</blockquote></td>"
			    +"</tr>"
		    $(tblRow2).appendTo("#comments tbody");
        }  
        function PopulateCommentReply(data){        
            var comment = data;
	        var postdate = jsonParseDate(comment.AddedDate); 
	        var displaydate = getCalendarDate(postdate) + " " + getClockTime(postdate);
		    var divreply = "<tr><td><div class='comment-header'>Reply posted by <span class='name'>"+comment.UserName+"</span> "+displaydate+"</div>";
		    divreply+="<blockquote class='body'>"+comment.Body+"</blockquote></td></tr>";
		    $(divreply).appendTo("#cmnt-"+comment.ParentCommentID+ " tbody");
        }                       
    </script>
}

